from clickhouse_sqlalchemy import make_session
from sqlalchemy import create_engine
import pandas as pd

conf={
    "user":"default",
    "password":"hubu88661126",
    "server_host":"111.47.28.118",
    "port":"9123",
    "db":"github_log"
}

def getConnection():
    engine=create_engine('clickhouse://{user}:{password}@{server_host}:{port}/{db}'.format(**conf))
    return engine

def Select_Pr_Count(actor_login):
    engine=getConnection()
    session=make_session(engine)
    sql="select pr_count from PA_pr_count where actor_login=('%s')"%(actor_login)
    cursor=session.execute(sql)
    try:
        jilu=cursor.fetchall()
        # fields=cursor._metadata.keys
        # df=pd.DataFrame([dict(zip(fields,item)) for item in cursor.fetchall()])
        return jilu[0][0]
    finally:
        cursor.close()
        session.close()
    return null

def Select_Pr_ReView_Count(actor_login):
    engine = getConnection()
    session = make_session(engine)
    sql="select pr_review_count from PA_pr_review_count where actor_login='%s'"%(actor_login);
    cursor=session.execute(sql)
    try:
        jilu=cursor.fetchall()
        if len(jilu)==0:
            return 0
        return jilu[0][0]
    finally:
        cursor.close()
        session.close()

    return null

def Select_Repos_Pr_AllCounts(actor_login):
    engine=getConnection()
    session=make_session(engine)
    sql="select forks from PA2_repos_pr_allcounts where ownerinfo_login='%s'"%(actor_login)
    cursor=session.execute(sql)
    try:
        jilu=cursor.fetchall()
        if len(jilu)==0:
            return 0
        return jilu[0][0]
    finally:
        cursor.close()
        session.close()

def Select100_Name(numbers):
    engine=getConnection()
    session=make_session(engine)
    sql="select actor_login from PA_pr_count limit "+str(numbers)
    cursor=session.execute(sql)
    authors=[]
    try:
        jilu=cursor.fetchall()
        for author in jilu:
            authors.append(author[0])
        return authors
    finally:
        cursor.close()
        session.close()
    return null


print(Select100_Name(10))

